﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;

namespace School.SystemManage.UserManage
{
    public partial class UserEdit : System.Web.UI.Page
    {
        protected string title = string.Empty;
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        bool isAdd = false;
        AdminUsers user = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var roles = db.Role;
                ddl_Role.DataSource = roles;
                ddl_Role.DataTextField = "RoleName";
                ddl_Role.DataValueField = "RID";
                ddl_Role.DataBind();
            }

            isAdd = Request.QueryString["action"] == "add";

            if (!isAdd)
            {
                title = "编辑后台管理人员";

                int uid = int.Parse(Request.QueryString["uid"]);
                user = db.AdminUsers.First(u => u.UserID == uid);

                if (!IsPostBack)
                {
                    txbUserName.Text = user.UserName;
                    string pwd = Security.Cryptography.AESDecrypt(user.UserPwd, "technology");
                    txbPwd.Attributes.Add("value", pwd);
                    txbConfim.Attributes.Add("value", pwd);
                    txbRealName.Text = user.RealName;
                    rblEnabled.SelectedValue = user.Enabled == true ? "1" : "0";
                    rblIsAdmin.SelectedValue = user.IsAdmin == true ? "1" : "0";
                    ddl_Role.SelectedValue = user.Role.RID.ToString();                  
                }
            }
            else
            {
                title = "添加后台管理人员";

                user = new AdminUsers();
            }
            
        }


        protected void tbnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = txbUserName.Text.Trim();
                if (isAdd)
                {                   
                    if (db.AdminUsers.FirstOrDefault(u => u.UserName == userName) != null)
                    {
                        msg.Text = "添加失败，该用户名已经存在！";
                        return;
                    }
                }

                user.UserName = userName;
                user.UserPwd = Security.Cryptography.AESEncrypt(txbPwd.Text.Trim(), "technology");
                user.RealName = txbRealName.Text.Trim();
                user.Enabled = (rblEnabled.SelectedValue == "1");
                user.IsAdmin = (rblIsAdmin.SelectedValue == "1");
                int rid = int.Parse(ddl_Role.SelectedValue);
                user.Role = db.Role.FirstOrDefault(r => r.RID == rid);

                if (isAdd)
                {
                    user.LoginTime = DateTime.Now;
                    db.AddToAdminUsers(user);
                }

                db.SaveChanges();

                Response.Redirect("UserList.aspx");
            }
            catch (Exception ex)
            {
                msg.Text = "添加失败，原因：" + ex.Message;
            }
        }
    }
}
